package student.aistar.util;

import student.aistar.model.entity.Collect;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @Description:
 * @Auther: zhugefeng
 * @Date:2021/10/24 15:35
 **/
public class ItemCFUtil {
    public ArrayList<Integer> results(Integer userIds, List<Collect> list, Integer productcount) {
        //统计商品总数
        Map<Integer, Integer> productMap = new LinkedHashMap<Integer, Integer>();
        //存放用户-收藏过的商品
        Map<Integer, ArrayList<Integer>> userMap = new LinkedHashMap<Integer, ArrayList<Integer>>();
        //存放每个商品被收藏的用户数量
        Map<Integer, Integer> productUserMap = new LinkedHashMap<Integer, Integer>();
        //存放相似度
        Map<ArrayList<Integer>, Double> similaryMap = new LinkedHashMap<ArrayList<Integer>, Double>();
        //存放兴趣度
        Map<Integer, Double> resultMap = new LinkedHashMap<Integer, Double>();


        for (Collect collect : list) {
            productMap.put(collect.getProductId(),1);
            Integer userId = collect.getUserId();
            Integer productId = collect.getProductId();
            //存放用户收藏过的商品
            //判断集合里是否存在此用户
            if (userMap.containsKey(userId)) {
                //已经存在
                ArrayList<Integer> productlist = userMap.get(userId);
                if (productlist.size() > 0 && productlist != null) {
                    productlist.add(collect.getProductId());
                    userMap.remove(userId);
                    userMap.put(userId, productlist);
                }
            } else {
                //不存在此用户
                ArrayList<Integer> productlist = new ArrayList<>();
                productlist.add(collect.getProductId());
                userMap.put(userId, productlist);
            }
            //存放每个商品被收藏的用户数量
            if (productUserMap.containsKey(productId)) {
                Integer count = productUserMap.get(productId);
                if (count != null && count >= 0) {
                    productUserMap.remove(productId);
                    productUserMap.put(productId, count + 1);
                }
            } else {
                productUserMap.put(productId, 1);
            }

        }
        //对每个商品的用户数进行存放
        //遍历建立矩阵
        int productArr[][] = new int[productcount + 1][productcount + 1];
        for (Map.Entry<Integer, ArrayList<Integer>> entry : userMap.entrySet()) {
            if (entry.getValue().size() >= 2) {
                for (int i = 0; i < entry.getValue().size() - 1; i++) {
                    for (int x = i + 1; x < entry.getValue().size(); x++) {
                        productArr[entry.getValue().get(i)][entry.getValue().get(x)] += 1;
                        productArr[entry.getValue().get(x)][entry.getValue().get(i)] += 1;
                    }
                }
            }
        }
        for (Map.Entry<Integer, Integer> entry : productMap.entrySet()) {
            Integer productId = entry.getKey();
            //判断是否为用户收藏过商品
            ArrayList<Integer> productList = userMap.get(userIds);
            if (productList.contains(productId)) {
                continue;
            } else {
                for (int i = 0; i < productList.size(); i++) {
                    int sum = productArr[productId][productList.get(i)];
                    int countx = productUserMap.get(productId);
                    int county = productUserMap.get(productList.get(i));
                    if (sum == 0 || countx == 0 || county == 0) {
                        continue;
                    }
                    double similarity = (double) sum / Math.sqrt((double) countx * county);
//                    System.out.println(productId+":"+similarity);
                    ArrayList<Integer> lists = new ArrayList<>();

                    lists.add(productList.get(i));
                    lists.add(productId);
                    similaryMap.put(lists, similarity);

                }
            }
        }
        ArrayList<Integer> interests = new ArrayList<>();
        for (Map.Entry<ArrayList<Integer>, Double> entry : similaryMap.entrySet()) {

//            System.out.println(entry.getKey()+":"+entry.getValue());
            Collect collect = new Collect();
            for (Collect collect1 : list) {
                if (collect1.getUserId() == userIds && collect1.getProductId() == entry.getKey().get(0)) ;
                collect = collect1;
            }
            if (resultMap.containsKey(entry.getKey().get(1))) {
//                System.out.println(resultMap.get(entry.getKey().get(1)));
//                System.out.println(entry.getValue());
                double interest = resultMap.get(entry.getKey().get(1)) + entry.getValue();
//                System.out.println("interest:"+interest);
                resultMap.remove(entry.getKey().get(1));
                resultMap.put(entry.getKey().get(1), interest);
            } else {
                double interest = entry.getValue();
                resultMap.put(entry.getKey().get(1), interest);
                interests.add(entry.getKey().get(1));
            }
        }
//        for (Map.Entry<Integer, Double> entry : resultMap.entrySet()) {
//            System.out.println(entry.getKey()+":"+entry.getValue());
//        }

        //排序
        Comparator comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (resultMap.get(o1) < resultMap.get(o2))
                    return 1;
                else return -1;
            }
        };
        Collections.sort(interests,comparator);
        return interests;
    }
}
